package com.sigmazero13.bgjeeves;

import java.util.List;

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.os.Bundle;
import android.util.Log;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView.AdapterContextMenuInfo;

import com.sigmazero13.bgjeeves.RenameAddDialog.OnRenameAddListener;

public class TeamList extends RenamableInfoList {

	public static int DIALOG_ADD = 1;
	
	private Integer gameId = 0;
	private RenameAddDialog addDialog;
	private String deleteTeam = "";
	private AlertDialog alertDialog;
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		Bundle bundle = getIntent().getExtras();
		gameId = bundle.getInt("game_id", 0);
				
		super.onCreate(savedInstanceState);
		
		registerForContextMenu(getListView());
		
		Log.i("TeamList", "gameId is " + gameId);
	}
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		MenuInflater inflater = getMenuInflater();
		inflater.inflate(R.menu.team_list_menu, menu);
		return true;
	}
	
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch(item.getItemId()) {
		case R.id.team_menu_add:
			showDialog(DIALOG_ADD);
			break;
		}
		
		return true;
	}
	
	@Override
	public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
		super.onCreateContextMenu(menu, v, menuInfo);
		MenuInflater inflater = getMenuInflater();
		inflater.inflate(R.menu.team_context_menu, menu);
	}
	
	@Override
	public boolean onContextItemSelected(MenuItem item) {
		AdapterContextMenuInfo info = (AdapterContextMenuInfo)item.getMenuInfo();
		String thisTeam = (String)getListView().getItemAtPosition(info.position);
		
		switch (item.getItemId()) {
		case R.id.team_rename:
			openRenameDialog(thisTeam);
			return true;
		case R.id.team_delete:
			deleteTeam = thisTeam;
			alertDialog = new AlertDialog.Builder(this)
				.setTitle(R.string.team_delete_title)
				.setMessage(R.string.team_delete_message)
				.setPositiveButton(R.string.team_delete_ok, new OnClickListener() {
					@Override
					public void onClick(DialogInterface dialog, int which) {
						deleteTeam();
					}
				})
				.setNegativeButton(R.string.team_delete_cancel, new OnClickListener() {
					@Override
					public void onClick(DialogInterface dialog, int which) {
						return;
					}
				})
				.create();
			alertDialog.show();
			return true;
		default:
			return super.onContextItemSelected(item);
		}
	}
	
	@Override 
	public Dialog onCreateDialog(int id, Bundle args) {
		if (id != DIALOG_ADD) return super.onCreateDialog(id, args);
		
		addDialog = new RenameAddDialog(this, getAddDialogTitle(), RenameAddDialog.DIALOG_MODE_ADD, getDialogRenameAddListener());
		
		return addDialog;
	}
	
	@Override
	public void onPrepareDialog(int id, Dialog d, Bundle args) {
		if (id != DIALOG_ADD) {
			super.onPrepareDialog(id, d, args);
			return;
		}
		
		((RenameAddDialog)d).setDefaultText("");
	}
	
	protected String getAddDialogTitle() {
		return getString(R.string.add_dialog_title_team);
	}

	@Override
	protected String getRenameDialogTitle() {
		return getString(R.string.rename_dialog_title_team);
	}
	
	@Override
	protected OnRenameAddListener getDialogRenameAddListener() {
		return new OnRenameAddListener() {
			@Override
			public void onRename(String origName, String newName) {
				((BoardGameUtilsApp)TeamList.this.getApplication()).getDbHelper().renameTeamForGame(origName, newName, gameId);
				updateList();
			}
			
			@Override
			public void onAdd(String newName) {
				((BoardGameUtilsApp)TeamList.this.getApplication()).getDbHelper().addTeamForGame(newName, gameId);
				updateList();
			}
		};
	}

	@Override
	protected List<String> getList() {
		return ((BoardGameUtilsApp)TeamList.this.getApplication()).getDbHelper().getTeamsForGame(gameId);
	}
	
	private void deleteTeam() {
		((BoardGameUtilsApp)TeamList.this.getApplication()).getDbHelper().deleteTeamForGame(deleteTeam, gameId);
		deleteTeam = "";
		updateList();
	}
}
